<template>
  <c-dialog ref="cDialog" title="方案" width="80vw" :open-fun="openFun" hide-btn @close="closeHandle">
    <Next v-if="survive" is-draw :area-special-zone="areaSpecialZone" :design-id="designId" :program-id="programId" :shop-total-area="shopTotalArea" @cancel="cancelHandle" @confirm="confirmHandle" />
  </c-dialog>
</template>
<script>
import { otherFiltersItem, findParentComponent, findChildComponent, deepClone } from '@/utils'
import Next from './addComponents/next'
import { getShopDesignInfo } from '@/api/shopDesignTask'
import Vue from 'vue'

Vue.mixin({
  created() {
    const route = this.$route || {}
    if (route.name !== 'ShopDrawingDetailManagementBCDrawing' && route.name !== 'ShopDrawingDetailManagementSGStoresDrawing') {
      return
    }
    if (!/^(TableBox)|(ElTable)$/.test((this.$options || {}).name)) return
    const c = findParentComponent(this.$parent, 'SchemeDraw')
    if (!c || !c.filtersDataFun) return
    // 第一次选中过滤重复
    if ('Soft Scene Product Model Customized Cupboard BathRoom'.includes(((this.$parent || {}).$options || {}).name)) {
      this.$watch('data', function(v) {
        c.filterSlectData(this)
      })
    }
    // 获取的数据过滤重复
    this.$nextTick(() => {
      c.filtersDataFun(this)
    })
  }
  // beforeDestroy() {
  //   this.otherFilters = deepClone(this._otherFilters)
  // }
})
export default {
  name: 'SchemeDraw',
  components: {
    Next
  },
  props: {
    detail: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      areaSpecialZone: [],
      programId: '',
      designId: '',
      shopTotalArea: {},
      survive: false,
      filtersData: {},
      repetitionArry: []
    }
  },
  computed: {
  },
  watch: {

  },
  methods: {
    confirmHandle(v) {
      this.$emit('on-change', v)
      this.$refs.cDialog.visible = false
    },
    cancelHandle() {
      this.$refs.cDialog.visible = false
    },
    openFun() {
      const { constCode } = this.detail
      if (!constCode) return false
      return new Promise((resolve, reject) => {
        getShopDesignInfo(constCode).then(res => {
          this.designId = res.data.id
          this.programId = res.data.selectedDesignId
          this.shopTotalArea = {
            softwareArea: res.data.softwareArea,
            suiteArea: res.data.suiteArea,
            sceneArea: res.data.sceneArea,
            bathroomArea: res.data.bathroomArea,
            sampleRoomArea: res.data.sampleRoomArea,
            wholeHouseCustomizedArea: res.data.wholeHouseCustomizedArea,
            wholeCabinetArea: res.data.wholeCabinetArea
          }
          this.survive = true
          this.$nextTick(() => {
            resolve(true)
          })
        }).catch(() => {
          reject(false)
        })
      })
    },
    openCDialog(v, t) {
      this.areaSpecialZone = v.map(e => e.areaCode)
      this.filtersData = t
      this.$refs.cDialog.clickHandle()
    },
    closeHandle() {
      this.survive = false
      this.areaSpecialZone = []
    },
    filterSlectData(child) {
      // console.log(child.data, '-=-=-=-=-=-=')
    },
    filtersDataFun(child) {
      if (child.$parent.$options.name !== 'ElDialog' || !/^AddProgram.*/.test(child.$parent.$parent.$options.name)) return
      child.$on('data-change', v => {
        const c = this.findComponentByProperty(this.$children, 'programsZoneOption', 'activeProgramIndex')
        const space = c.programsZoneOption[c.activeProgramIndex] || {}
        const t = this.$parent.showTableData
        child.tableData = child.tableData.filter(d => !t.some(e => {
          let result = true
          result = e.areaCode === d.areaCode
          if (e.spaceCode && space.spaceCode) {
            result = result && e.spaceCode === space.spaceCode
          }
          if (e.styleCode && d.styleCode) {
            result = result && e.styleCode === d.styleCode
          }
          if (e.seriesCode && d.seriesCode) {
            result = result && e.seriesCode === d.seriesCode
          }
          return result
        }))
      })
    },
    findComponentByProperty(children, ...args) {
      for (const c of children) {
        if (args.every(e => c[e] !== undefined)) {
          return c
        }
        if (c.$children) {
          const t = this.findComponentByProperty(c.$children, ...args)
          if (t) {
            return t
          }
        }
      }
      return false
    }
  }
}
</script>
<style lang="scss" scoped>
.nctb{
  /deep/ .pagination-container{
    display: none !important;
  }
}
</style>
<style lang="scss">
.scheme{
  .btn{
    display: none !important;
  }
}
</style>
